package cn.sjxy.config.mvcExtend;

import lombok.Data;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 字符串转日期的转换器
 */
@Data
public class StringToDateConverter implements Converter<String, Date> {
    //日期的字符串格式
    private String datePattern;
    @Override
    public Date convert(String s) {
        if (s == null || "".equals(s.trim()) || "null".equals(s.trim())) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
        Date result = null;
        try {
            result = sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("转换失败");
        }
        return result;
    }
}

